package popple.core.renders
{
	import flash.display3D.Context3D;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.IndexBuffer3D;
	import flash.display3D.VertexBuffer3D;
	import flash.display3D.textures.Texture;
	import flash.geom.Matrix3D;
	
	import popple.cameras.Camera;

	public class NormalRender extends BaseRender
	{
		protected var mUVBuffer:VertexBuffer3D;
		protected var mVtBuffer:VertexBuffer3D;
		
		protected var mTexture:Texture;
		public function NormalRender(context:Context3D)
		{
			super(context);
		}
		override protected function setAGALCode():void
		{
			mVertexSource=
				"m44 op, va0, vc0\n" +
				
				"mov v0,va1\n";
			
			mFragmentSource=
				"tex ft1, v0, fs0 <2d,clamp,linear> \n"+
				"mul ft1,ft1,fc0\n"+
				"mov oc ft1";
		}
		public function render(
							   vtBuffer:VertexBuffer3D,
							   uvBuffer:VertexBuffer3D,
							   idexBuffer:IndexBuffer3D,
							   texture:Texture,
							   color:Vector.<Number>,
							   camera:Matrix3D,
		                       worldMatrix:Matrix3D):void
		{
			
             if(mVtBuffer!=vtBuffer)
			 {
				 context.setVertexBufferAt(0,vtBuffer,0,"float3");
				 mVtBuffer=vtBuffer;
			 }
			
			 if(mUVBuffer!=uvBuffer)
			 {
				 context.setVertexBufferAt(1,uvBuffer,0,"float2");
				 mUVBuffer=uvBuffer;
			 }
			 if(mTexture!=texture)
			 {
				 context.setTextureAt(0,texture);
				 mTexture=texture;
			 }
			 worldMatrix.append(Camera.current.capture());
			
			 context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX,0,worldMatrix,true);
			 context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT,0,color);
			 context.drawTriangles(idexBuffer,0,2);
		}
		
	}
}